import cTitle from "components/title";
import { scrollMixin } from "../../utils/mixin"; //引入加载更多
import { Toast } from 'vant';
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active:0,
      address: "",
      city:"",
      point:"",
      title:'',
      worker_id:'',
      worker:'',
      store:'',
      projects:'',
      // more
      isLoadMore: true,
      page: 1,
      total_page: 0,
      titleInfo: '', // 标题
      commentList:[],
      pullDownBtnShow:true
    };
  },
  computed: {
    appointmentLang() {
      if(this.$store.state.shopLanguage.appointment) {
        return this.$store.state.shopLanguage.appointment;
      }else {
        return {worker: "技师", project: "项目", service: "服务"};
      }
    },
  },
  activated() {
    if(this.$route.params.worker_id){
      this.worker_id = this.$route.params.worker_id;
    }
    this.ready();
    let that = this;
    setTimeout(() => {
      window.scrollTo(0, 0);
      that.display = false;
    }, 20);
  },
  methods: {
    goProject(id){
      this.$router.push(this.fun.getUrl("ProjectDetails", { project_id: id }));
    },
    pullDownBtn(){
      this.pullDownBtnShow = !this.pullDownBtnShow;
    },
    toUrl(str, params) {
      this.$router.push(this.fun.getUrl(str,params));
    },
    tabDataBtn(e){
      if( e==2 && this.commentList==0){
        this.getComment();
      }
    },
    //获取更多数据
    getMoreData() {
      if(this.active != 2) {
        return;
      }
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http
          .get(
            "plugin.appointment.frontend.comment.get-comment-by-worker-id",
            {
              worker_id:this.worker_id,
              page:this.page
            },
            "加载中"
          )
          .then(
            function(response) {
              that.isLoadMore = true;
              if (response.result === 1) {
                var myData = response.data.data;
                that.commentList = that.commentList.concat(myData); //数组拼接
              } else {
                that.page = that.page - 1;
                that.isLoadMore = false;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    },
    getComment(){
      $http
        .get(
          "plugin.appointment.frontend.comment.get-comment-by-worker-id",
          {
            worker_id:this.worker_id,
            page:this.page
          },
          "获取中"
        )
        .then(response => {
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.commentList =  response.data.data;
          } else {
            this.$dialog.alert({message:response.msg});
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getData() {
      $http
        .get(
          "plugin.appointment.frontend.worker.get-detail-by-id",
          {
            lng:this.point.lng,
            lat:this.point.lat,
            worker_id:this.worker_id
          },
          "获取中"
        )
        .then(response => {
          this.fun.setWXTitle(this.appointmentLang.worker+"详情");
          if (response.result === 1) {
            this.worker = response.data.worker;
            this.store = response.data.store;
            this.projects = response.data.projects;
          } else {
            this.$dialog.alert({message:response.msg});

          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    ready() {
      // let myLocation = this.$store.state.o2oLocation;
      this.initData();
      // if (myLocation.point) {
      //   this.address = myLocation.title;
      //   this.city = myLocation.city;
      //   this.point = myLocation.point;
      //   //初始化数据
      //   this.getData();
      // } else {
      //   console.log("location不存在");
      // }
      this.fun.getLocation()
        .then((res)=> {
          this.point = res.point;
          this.address = res.title;
          this.city = res.city;
          this.getData();
        })
        .catch((err)=> {
          console.log(err);
        });
    },
    initData() {
      this.active = 0;
      this.isLoadMore= true;
      this.page= 1;
      this.total_page= 0;
      this.titleInfo= '';
      this.commentList=[];
      this.pullDownBtnShow=true;
    },
    toRouter(id) {
      this.$router.push(this.fun.getUrl("HomeSeller", { store_id: id, fromHome: 1 }));
    },
  },
  components: { cTitle }
};
